package translate;

import java.util.ArrayList;

import tree.BasicExp;
import tree.BasicStm;
import tree.ESEQ;
import tree.SEQ;

public class LetExp extends Ex {
	SEQ cS = null;
	ArrayList<Exp> decs = new ArrayList<Exp>();
	Exp body;
	
	public LetExp() {
		super(null);
	}
	
	public void addDec(Exp d) {
		decs.add(d);
	}
	
	public void setBody(Exp b) {
		body = b;
	}
	
	BasicExp unEx() {
		SEQ tail, head;
		if (decs.size() > 1) {
			tail = head = new SEQ(decs.get(0).unNx(), null);
			for (int i = 1; i < decs.size() - 1; i++) {
				tail.right = new SEQ(decs.get(i).unNx(), null);
				tail = (SEQ)tail.right;
			}
			tail.right = decs.get(decs.size() - 1).unNx();
			return new ESEQ(head, body.unEx());
		}
		else 
			if (decs.size() == 1)
				return new ESEQ(decs.get(0).unNx(), body.unEx());
		else return body.unEx();
	}
	
	BasicStm unNx() {
		SEQ tail, head;
		if (decs.size() > 1) {
			tail = head = new SEQ(decs.get(0).unNx(), null);
			for (int i = 1; i < decs.size() - 1; i++) {
				tail.right = new SEQ(decs.get(i).unNx(), null);
				tail = (SEQ)tail.right;
			}
			tail.right = decs.get(decs.size() - 1).unNx();
			return new SEQ(head, body.unNx());
		}
		else 
			if (decs.size() == 1)
				return new SEQ(decs.get(0).unNx(), body.unNx());
		else return body.unNx();
	}
}
